package com.lagou.edu.controller;

import com.lagou.edu.dao.ResumeDao;
import com.lagou.edu.pojo.Resume;
import com.lagou.edu.util.LoginUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author 应癫
 */
@Controller
@RequestMapping("/test")
public class TestController {


    @Autowired
    private ResumeDao resumeDao;
    @Autowired
    private LoginUtil loginUtil;


    @RequestMapping("/tologin")
    public ModelAndView toLogin(HttpServletRequest request, HttpServletResponse response, String uname, String password) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("login");
        return modelAndView;
    }
    @RequestMapping("/login")
    public ModelAndView login(HttpServletRequest request, HttpServletResponse response, String uname, String password) {
        if (!"admin".equals(uname) && !"admin".equals(password)) {

        }
        ConcurrentHashMap<String, String> session = loginUtil.getSession();
        String sessionId = UUID.randomUUID().toString();
        session.put(sessionId,uname);
        Cookie cookie = new Cookie("sessionId",sessionId);
        response.addCookie(cookie);
        // 跳转到
        ModelAndView modelAndView = new ModelAndView();
        List<Resume> resumeList = resumeDao.findAll();
        modelAndView.addObject("resumeList",resumeList);
        modelAndView.setViewName("resume");
        return modelAndView;
    }

    @RequestMapping("/resume")
    public ModelAndView resume(HttpServletRequest request, HttpServletResponse response, String uname, String password) {
        ModelAndView modelAndView = new ModelAndView();
        List<Resume> resumeList = resumeDao.findAll();
        modelAndView.addObject("resumeList",resumeList);
        modelAndView.addObject("a","asasasa");
        modelAndView.setViewName("resume");
        return modelAndView;
    }
    @RequestMapping("/toadd")
    public ModelAndView toadd(HttpServletRequest request, HttpServletResponse response, String uname, String password) {
        ModelAndView modelAndView = new ModelAndView();
        List<Resume> resumeList = resumeDao.findAll();
        modelAndView.addObject("resumeList",resumeList);
        modelAndView.addObject("a","asasasa");
        modelAndView.setViewName("add");
        return modelAndView;
    }
    @RequestMapping("/add")
    @ResponseBody
    public void add(@RequestBody Resume resume) {
        resumeDao.save(resume);
    }

    @RequestMapping("/toupdate")
    public ModelAndView toupdate(Long id) {
        Optional<Resume> byId = resumeDao.findById(id);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("update");
        modelAndView.addObject("resume",byId.get());
        return modelAndView;
    }

    @RequestMapping("/update")
    @ResponseBody
    public void update(@RequestBody Resume resume) {
        resumeDao.update(resume.getAddress(), resume.getName(), resume.getPhone(), resume.getId());
    }
    @RequestMapping("/todelete")
    public ModelAndView todelete(Long id) {
        resumeDao.deleteById(id);
        List<Resume> resumeList = resumeDao.findAll();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("resumeList",resumeList);
        modelAndView.addObject("a","asasasa");
        modelAndView.setViewName("resume");
        return modelAndView;
    }


}
